using ReaLTaiizor.Colors;
using ReaLTaiizor.Forms;
using ReaLTaiizor.Manager;
using ReaLTaiizor.Util;

namespace 人员管理4
{
    public partial class Form1 : MaterialForm
    {
        internal PersonUtils personUtils;
        private long selId;
        public Form1()
        {
            InitializeComponent();
            
            personUtils = new PersonUtils();

        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            string name = tb_name.Text;
            string age = tb_age.Text;
            string sex = cb_sex.Text;
            string addr = tb_addr.Text;

            //MessageBox.Show($"{name},{age},{sex},{addr}");

            bool b = personUtils.InsertPerson(name, age, sex, addr);
            if (b)
            {
                RefreshList();
                ClearEddit();
            }
        }

        private void ClearEddit()
        {
            tb_name.Text = "";
            tb_age.Text = "";
            cb_sex.Text = "";
            tb_addr.Text = "";
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            string name = tb_name.Text;
            string age = tb_age.Text;
            string sex = cb_sex.Text;
            string addr = tb_addr.Text;

            bool b = personUtils.EditPerson(selId, name, age, sex, addr);
            if (b)
            {
                RefreshList();
            }
        }

        private void uiButton3_Click(object sender, EventArgs e)
        {
            bool b = personUtils.DeletePerson(selId);
            if (b)
            {
                RefreshList();
            }
        }

        private void uiButton4_Click(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 选项卡切换的时候触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void metroTabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = metroTabControl1.SelectedIndex;
            MessageBox.Show($"{index}");
            if (index == 0)
            {


            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            RefreshList();
        }

        private void RefreshList()
        {
            IEnumerable<Person> enumerable = personUtils.SelectAll();
            dgv1.DataSource = enumerable;
        }

        private void dgv1_SelectionChanged(object sender, EventArgs e)
        {
            int count = dgv1.SelectedRows.Count;
            if (count > 1)
            {
                MessageBox.Show($"你选择了{count}条记录，只能选择一条");
            }
            else if (count == 1)
            {
                DataGridViewRow row = dgv1.SelectedRows[0];
                if (row != null)
                {
                    foreach (DataGridViewCell c in row.Cells)
                    {
                        int index = c.ColumnIndex;
                        object value = c.Value;
                        if (value != null)
                        {
                            if (index == 0)
                            {
                                selId = Convert.ToInt32(c.Value);
                            }
                            else if (index == 1)
                            {
                                tb_name.Text = c.Value.ToString();
                            }
                            else if (index == 2)
                            {
                                tb_age.Text = c.Value.ToString();
                            }
                            else if (index == 3)
                            {
                                cb_sex.Text = c.Value.ToString();
                            }
                            else if (index == 4)
                            {
                                tb_addr.Text = c.Value.ToString();
                            }
                        }
                    }
                }
            }
        }
    }
}
